﻿package cse481d
{
    import cgs.server.logging.*;
    import cgs.server.logging.actions.*;
    import flash.events.*;
    import flash.net.*;

    public class Logger extends Object
    {
        private var server:CGSServer;
        private var lastQid:int = -1;
        private var levelStartTime:Number;

        public function Logger()
        {
            return;
        }// end function

        public function logLevelStart(param1:int, param2:Object) : void
        {
            if (param1 <= 0)
            {
                throw new ArgumentError("qid must be positive");
            }
            this.lastQid = param1;
            this.server.logQuestStart(param1, param2);
            this.levelStartTime = new Date().time;
            return;
        }// end function

        public function logAction(param1:int, param2:Object) : void
        {
            if (param1 <= 0)
            {
                throw new ArgumentError("aid must be positive");
            }
            var _loc_3:* = new ClientAction(param1, new Date().time - this.levelStartTime);
            _loc_3.setDetail(param2);
            this.server.logQuestAction(_loc_3);
            return;
        }// end function

        public function logLevelEnd(param1:Object) : void
        {
            if (this.lastQid == -1)
            {
                throw new ArgumentError("no active level");
            }
            this.lastQid = -1;
            this.server.logQuestEnd(this.lastQid, param1);
            return;
        }// end function

        private static function loadUrl(param1:URLRequest, param2:Function) : void
        {
            var loader:URLLoader;
            var onLoad:Function;
            var onError:Function;
            var req:* = param1;
            var callback:* = param2;
            onLoad = function (event:Event) : void
            {
                loader.removeEventListener(Event.COMPLETE, onLoad);
                callback((event.target as URLLoader).data);
                return;
            }// end function
            ;
            onError = function (event:IOErrorEvent) : void
            {
                loader.removeEventListener(IOErrorEvent.IO_ERROR, onError);
                callback(null);
                return;
            }// end function
            ;
            loader = new URLLoader();
            loader.addEventListener(Event.COMPLETE, onLoad);
            loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
            try
            {
                loader.load(req);
            }
            catch (e:Error)
            {
                Logger.callback(null);
            }
            return;
        }// end function

        public static function initialize(param1:int, param2:String, param3:String, param4:int, param5:Object) : Logger
        {
            var logger:Logger;
            var gid:* = param1;
            var name:* = param2;
            var skey:* = param3;
            var cid:* = param4;
            var data:* = param5;
            if (gid <= 0 || name == null || skey == null)
            {
                throw new ArgumentError("invalid game info");
            }
            logger = new Logger;
            var DOLOG_URL:* = "" + gid.toString() + "&code=304236658355552";
            logger.server = new CGSServer();
            var props:* = new CGSServerProps(skey, 0, name, gid, 1, cid, CGSServerConstants.DEV_URL, false, null);
            logger.server.init(props, false);
            logger.server.requestUUID(function (param1:String, param2:Boolean) : void
            {
                logger.server.logPageLoad(data);
                return;
            }// end function
            , true);
            var request:* = new URLRequest(DOLOG_URL);
            request.method = "GET";
            loadUrl(request, function (param1:String) : void
            {
                if (param1 == null || param1.charAt(0) != "1")
                {
                    logger.server.disableLogging();
                }
                return;
            }// end function
            );
            return logger;
        }// end function

    }
}
